package com.mcptt.http.operator.addr;


import java.util.List;
import java.util.Vector;

import com.cloudptt.api.product.log.Log;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mcptt.config.local.SysConfig;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.GroupInfo;
import com.mcptt.db.model.UserInfo;

public class AddressParser
{
	private SysConfig config = null;
	List<GroupInfo> groupList = null;
	List<UserInfo> userList = null;
	public AddressParser(SysConfig config)
	{
		this.config = config;
		groupList = new Vector<GroupInfo>();
		userList = new Vector<UserInfo>();
	}
	public boolean parse(String jsonStr) {
		JsonParser jsonParser = new JsonParser();
		JsonObject obj = jsonParser.parse(jsonStr).getAsJsonObject();
		Log.debug(getClass(), "onResponse obj = " + obj);
		int cause = obj.get("Cause").getAsInt();

		Log.debug(getClass(), "onResponse cause = " + cause);

		if(cause == 0)
		{
			int gisInterval = obj.get("GisInterval").getAsInt();
			Log.debug(getClass(), "onResponse gisInterval = " + gisInterval);
			config.setGisInterval(gisInterval);

			if(obj.get("GroupList") != null)
			{
				JsonArray grpList = obj.get("GroupList").getAsJsonArray();
				Log.debug(getClass(), "onResponse grpList = " + grpList);
				for (int i = 0; i < grpList.size(); i++) {
					JsonObject jsonObject = grpList.get(i).getAsJsonObject();
					Log.debug(getClass(), "onResponse jsonObject = " + jsonObject);


					int groupType = AddrInfo.GroupType_Normal;// = jsonObject.get("group_type").getAsInt();
					String groupName = jsonObject.get("GrpAlias").getAsString();
					String groupId = jsonObject.get("GrpID").getAsString();
					int level = jsonObject.get("GrpPrio").getAsInt();
					int timer = jsonObject.get("FloorTime").getAsInt();
					int groupLevel = jsonObject.get("IsAllCall").getAsInt();

					GroupInfo groupInfo = new GroupInfo(groupType, groupName, groupId, GroupInfo.Group_Id_Root, level,timer,groupLevel);
					Log.debug(getClass(), "onResponse groupInfo = " + groupInfo);
					groupList.add(groupInfo);
					if (jsonObject.get("GList") != null) {
						JsonArray subGrpArr = jsonObject.get("GList").getAsJsonArray();
						parserGList(subGrpArr, groupId);
					}
					if (jsonObject.get("UList") != null) {
						JsonArray userArr = jsonObject.get("UList").getAsJsonArray();
						parserUList(userArr, groupId);
					}
				}
			}
			if(obj.get("UList") != null)
			{
				JsonArray userArr = obj.get("UList").getAsJsonArray();
				parserUList(userArr,GroupInfo.Group_Id_Root);
			}
			return true;
		}
		return false;
	}
	private void parserGList(JsonArray subGrpArr,String groupId)
	{

		//					Log.debug(getClass(), "onResponse subGrpArr = " + subGrpArr);
		for (int j = 0; j < subGrpArr.size(); j++) {
			JsonObject subGrpJsonObject = subGrpArr.get(j).getAsJsonObject();
			int type = AddrInfo.GroupType_Normal;// = jsonObject.get("group_type").getAsInt();
			String name = subGrpJsonObject.get("SubGrpAlias").getAsString();
			String id = subGrpJsonObject.get("SubGrpID").getAsString();
			int level = subGrpJsonObject.get("SubGrpPrio").getAsInt();
			int timer = subGrpJsonObject.get("FloorTime").getAsInt();
			int groupLevel = subGrpJsonObject.get("IsAllCall").getAsInt();

			GroupInfo subGrpInfo = new GroupInfo(type, name, id, groupId,level,timer,groupLevel);
			//						Log.debug(getClass(), "onResponse subGrpInfo = " + subGrpInfo);
			groupList.add(subGrpInfo);
			if (subGrpJsonObject.get("GList") != null)
			{
				JsonArray newSubGrpArr = subGrpJsonObject.get("GList").getAsJsonArray();
				parserGList(newSubGrpArr,groupId);
			}
			if(subGrpJsonObject.get("UList") != null)
			{
				JsonArray userArr = subGrpJsonObject.get("UList").getAsJsonArray();
				//						Log.debug(getClass(), "onResponse userArr = " + userArr);
				parserUList(userArr,id);
			}
		}
	}


	private void parserUList(JsonArray userArr,String groupId)
	{
		for (int k = 0; k < userArr.size(); k++) {
			JsonObject userJsonObject = userArr.get(k).getAsJsonObject();
			int type = AddrInfo.UserType_User;// = jsonObject.get("group_type").getAsInt();
			String name = userJsonObject.get("UAlias").getAsString();
			String id = userJsonObject.get("UID").getAsString();
			int state = userJsonObject.get("UState").getAsInt();
			int val = userJsonObject.get("UAttr").getAsInt();
			if(val / 10 == 3)
			{
				type = AddrInfo.UserType_Dac;
			}
			Log.debug(getClass(), "onResponse type = " + type + " val = " + val);

			UserInfo info = new UserInfo(type, name, id, groupId);
			info.setAttr(val % 10);
			info.setOnlineState(state);
			//			Log.debug(getClass(), "onResponse info = " + info);
			userList.add(info);
		}
	}

	public String toString()
	{
		StringBuffer buf = new StringBuffer();
		buf.append("groupList = ").append(groupList).append('\n');
		buf.append("userList = ").append(userList).append('\n');

		return buf.toString();
	}

}
